package com.lanou.bookstore.user.web.servlet;

import com.lanou.bookstore.user.domain.User;
import com.lanou.bookstore.user.service.RegisterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet",urlPatterns = "/register.do")
public class RegisterServlet extends HttpServlet {
   private RegisterService service=new RegisterService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        // todo 判断登录名重复
        User user=new User();
        user.setUsername(username).setPassword(password);
        if (service.register(user)){
        response.sendRedirect("/jsps/user/login.jsp");
        }else {
            String msg="注册失败";
        }
    }
//    private void fieldMsg(HttpServletRequest request, HttpServletResponse response, String msg) throws ServletException, IOException {
//        // String msg = "用户名/密码错误,点击返回登录页面";
//        request.setAttribute("msg", msg);
////        String path = "/register.jsp";
////        request.setAttribute("path", path);
//          response.setStatus(302);
////        response.setHeader("refresh", "5;url=/register.jsp");
//        request.getRequestDispatcher("/jsps/msg.jsp").forward(request, response);
//    }





    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
    }
}
